<?php

class FormGroup extends FormComponent
{

  protected $elements = array();

  protected $elementsIterator = array();

  /**
   *
   * @param string $name
   * @param FormElement $element
   * @return FormElement
   */
  public function addElement(FormComponent $element, $actions = Form::ACTION_ALL)
  {
    if (! ($this->form instanceof Form)) {
      throw new Exception('Before adding elements to a FormGroup, you must add the group to a Form');
    }

    $element->form = $this->form;
    $element->add = $actions;

    $this->elements[] = $element;

    return $element;
  }

  public function injectQueryParams(&$params)
  {
    $elements = $this->getElements();

    while ($element = $elements->next()) {
      $element->injectQueryParams($params);
    }
  }

  public function setDataFromPost(&$row, $post, $action)
  {
    $elements = $this->getElements();

    while ($element = $elements->next()) {
      if ($element instanceof FormElement && $element->getField() == $this->form->pk) {
        continue;
      }

      $element->setDataFromPost($row, $post, $action);
    }
  }

  /**
   *
   * @return FormComponentIterator
   */
  public function getElements($action = null, $reset = true)
  {
    if (empty($this->elementsIterator[$action])) {
      $this->elementsIterator[$action] = new FormComponentIterator($this->elements, $action);
    }

    if ($reset) {
      $this->elementsIterator[$action]->reset();
    }

    return $this->elementsIterator[$action];
  }

  public function render($row, $action)
  {
    $this->beforeRender($row, $action);

    $elements = $this->getElements($action);

    if (s::request()->json()) {
      $data = $data->getAll();

      $_elements = array();
      while ($element = $elements->next()) {
        $_elements[] = array('name' => $element->getName(), 'title' => $element->getTitle());
      }
      $elements = $_elements;
    }

    if (! s::request()->ajax() && ! s::request()->json()) {
      $this->set('pk', $this->form->pk);
      $this->set('form', $this->form);
      $this->set('action', $action);
      $this->set('title', $this->getTitle());
      $this->set('route', $this->form->getRoute());
      $this->set('action_title', $this->getTitle());
      $this->set('module_title', $this->form->getTitle());
      $this->set('filters', $this->form->getFilters());
    }

    $this->set('obj', $row);
    $this->set('elements', $elements);

    $output = $this->getView()->render();

    $this->afterRender($output, $row, $action);

    return $output;
  }

  public function getTemplate()
  {
    $template = parent::getTemplate();

    if (empty($template)) {
      global $config;

      $template = $config['views_dir'] . '/form/form_group_view.php';
    }

    return $template;
  }

  public function executeCallback($callback)
  {
    $elements = $this->getElements($action);

    while ($element = $elements->next()) {
      $args = func_get_args();
      call_user_func_array(array($element, 'executeCallback'), $args);
    }
  }

}

?>